﻿using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// LeftMenu.xaml 的交互逻辑
    /// </summary>
    public partial class LeftMenu : UserControl
    {
        public LeftMenu()
        {
            this.Items = new LeftMenuItemCollection() { Parent = this };
            InitializeComponent();
            content.ItemsSource = Items;
        }
        /// <summary>
        /// 添加一个子项并返回当前集合
        /// </summary>
        /// <param name="title"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public LeftMenuItemCollection AddItem(string title, object tag = null)
        {
            var item = new LeftMenuItem(title, tag);
            this.Items.Add(item);
            return this.Items;
        }
        /// <summary>
        /// 添加一个子项并返回该子项的集合
        /// </summary>
        /// <param name="title"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        public LeftMenuItemCollection AddItemContainer(string title, object tag = null)
        {
            var item = new LeftMenuItem(title, tag);
            this.Items.Add(item);
            return item.Items;
        }

        public static readonly DependencyProperty HeaderBackgroundProperty =
            DependencyProperty.Register("HeaderBackground", typeof(Brush), typeof(LeftMenu), new PropertyMetadata(Brushes.LightGray));

        public static readonly DependencyProperty ItemBackgroundProperty =
            DependencyProperty.Register("ItemBackground", typeof(Brush), typeof(LeftMenu), new PropertyMetadata(Brushes.WhiteSmoke));

        public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.Register("Items", typeof(LeftMenuItemCollection), typeof(LeftMenu), new PropertyMetadata(null));


        public static readonly DependencyProperty HeaderFontSizeProperty =
          DependencyProperty.Register("HeaderFontSize", typeof(double), typeof(LeftMenu), new PropertyMetadata(15d));

        public static readonly DependencyProperty ItemFontSizeProperty =
            DependencyProperty.Register("ItemFontSize", typeof(double), typeof(LeftMenu), new PropertyMetadata(13d));

        public static readonly DependencyProperty HeaderForegroundProperty =
            DependencyProperty.Register("HeaderForeground", typeof(Brush), typeof(LeftMenu), new PropertyMetadata(Brushes.Black));

        public static readonly DependencyProperty ItemForegroundProperty =
            DependencyProperty.Register("ItemForeground", typeof(Brush), typeof(LeftMenu), new PropertyMetadata(Brushes.Black));

        public Brush ItemForeground
        {
            get { return (Brush)GetValue(ItemForegroundProperty); }
            set { SetValue(ItemForegroundProperty, value); }
        }

        public Brush HeaderForeground
        {
            get { return (Brush)GetValue(HeaderForegroundProperty); }
            set { SetValue(HeaderForegroundProperty, value); }
        }

        public double HeaderFontSize
        {
            get { return (double)GetValue(HeaderFontSizeProperty); }
            set { SetValue(HeaderFontSizeProperty, value); }
        }

        public double ItemFontSize
        {
            get { return (double)GetValue(ItemFontSizeProperty); }
            set { SetValue(ItemFontSizeProperty, value); }
        }

        public Brush HeaderBackground
        {
            get { return (Brush)GetValue(HeaderBackgroundProperty); }
            set { SetValue(HeaderBackgroundProperty, value); }
        }

        public Brush ItemBackground
        {
            get { return (Brush)GetValue(ItemBackgroundProperty); }
            set { SetValue(ItemBackgroundProperty, value); }
        }

        public LeftMenuItemCollection Items
        {
            get { return (LeftMenuItemCollection)GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }
        /// <summary>
        /// 在项背点击时触发 
        /// </summary>
        public event ItemTriggerEventHandler ItemTrigger;

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            Expander EXP = (Expander)sender;
            var item = (LeftMenuItem)EXP.DataContext;
            ItemTrigger?.Invoke(this, new ItemTriggerEventArgs() { Item = item, TriggerType = ItemTriggerType.Enpanded });
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            Expander EXP = (Expander)sender;
            var item = (LeftMenuItem)EXP.DataContext;
            ItemTrigger?.Invoke(this, new ItemTriggerEventArgs() { Item = item, TriggerType = ItemTriggerType.Collapsed });

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var btn = (Button)sender;
            var item = (LeftMenuItem)btn.DataContext;
            ItemTrigger?.Invoke(this, new ItemTriggerEventArgs() { Item = item, TriggerType = ItemTriggerType.Click });
        }
    }
    public class ItemTriggerEventArgs : EventArgs
    {
        public LeftMenuItem Item { get; set; }

        public ItemTriggerType TriggerType { get; set; }
    }

    public enum ItemTriggerType
    {
        Collapsed,
        Enpanded,
        Click
    }
    public delegate void ItemTriggerEventHandler(object sender, ItemTriggerEventArgs e);

    public class MultiplyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            double result = 1.0;
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] is double)
                    result *= (double)values[i];
            }

            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new Exception("Not implemented");
        }
    }
}
